package com.yaorange.module.association.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.module.association.service.BsApplyService;
import com.yaorange.module.association.vo.BsMemberVO;
import com.yaorange.pojo.BsApply;
import com.yaorange.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 入会申请记录Controller类
 */
@RestController
@RequestMapping("/bsApply")
@Slf4j
public class BsApplyController {

    @Autowired
    private BsApplyService bsApplyService;

    /**
     * 查询所有记录，并按id降序排列
     *
     * @param bsApply 查询条件
     * @return 响应结果
     */
    @GetMapping
    public Result selectAll(@ModelAttribute BsApply bsApply) {
        List<BsApply> list = bsApplyService.selectAll(bsApply);
        return Result.success(list);
    }

    /**
     * 分页查询记录
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @param bsApply  查询条件
     * @return 响应结果
     */
    @GetMapping("/page")
    public Result selectPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ModelAttribute BsApply bsApply) {
        log.info("分页查询记录，当前页码：{}，每页大小：{}，查询条件：{}", pageNum, pageSize, bsApply);
        PageHelper.startPage(pageNum, pageSize);
        List<BsApply> list = bsApplyService.selectPage(pageNum, pageSize, bsApply);
        PageInfo<BsApply> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 根据主键查询记录
     *
     * @param id 主键
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id) {
        BsApply bsApply = bsApplyService.selectById(id);
        if (bsApply == null) {
            return Result.fail("记录不存在");
        }
        return Result.success(bsApply);
    }

    /**
     * 插入记录
     *
     * @param bsApply 记录对象
     * @return 响应结果
     */
    @PostMapping
    public Result insert(@RequestBody BsApply bsApply) {
        int rows = bsApplyService.insert(bsApply);
        if (rows > 0) {
            return Result.success();
        }
        return Result.fail("插入失败");
    }

    /**
     * 更新记录
     *
     * @param bsApply 记录对象
     * @return 响应结果
     */
    @PutMapping
    public Result updateById(@RequestBody BsApply bsApply) {
        int rows = bsApplyService.updateById(bsApply);
        if (rows > 0) {
            return Result.success();
        }
        return Result.fail("更新失败");
    }

    /**
     * 根据主键删除记录
     *
     * @param id 主键
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id) {
        int rows = bsApplyService.deleteById(id);
        if (rows > 0) {
            return Result.success();
        }
        return Result.fail("删除失败");
    }
    /**
     * 审核入会申请
     *
     * @param bsApply 待审核的入会申请
     * @return 响应结果
     */
    @PutMapping("/audit")
    public Result audit(@RequestBody BsApply bsApply) {
        log.info("审核入会申请：{}", bsApply);
        bsApplyService.audit(bsApply);
        return Result.success("已完成审核");
    }

    /**
     * 分配账号
     * @param bsMemberVO
     * @return
     */
    @PostMapping("/allocate")
    public Result allocate(@RequestBody BsMemberVO bsMemberVO) {
        log.info("分配账号：{}", bsMemberVO);
        bsApplyService.allocate(bsMemberVO);
        return Result.success("分配成功");
    }
}
